﻿using System.Collections;
using System.Collections.Generic;
using TouchVoid.BuzzThree;
using UnityEngine;

namespace JEngine
{
    //事件类型
    public enum ClientEventType
    {
        ReturnGame,//从后台切回游戏
        OpenEmailPanel,
        OpenPrivacyPolicyPanel,
        OpenSettingPanel,
        HidenSetingBtn,
        OpenUserAgreementPanel,
        ShowSettingBtn,
    }

    //客户端事件监听器
    public interface IClientEventListener
    {
        void OnClientEventHandler(ClientEventType type, params object[] param);//客户端事件处理方法
    }

    public class EventManager : GlobalObject<EventManager>
    {
        /// 针对客户端本地通知
        private Dictionary<ClientEventType, List<IClientEventListener>> _clientEventListener = new Dictionary<ClientEventType, List<IClientEventListener>>();

        //注册
        public void RegisterClientEventListener(ClientEventType type, IClientEventListener listener)
        {
            if (_clientEventListener.ContainsKey(type))
            {
                bool hasRegistered = false;
                for (int i = 0; i < _clientEventListener[type].Count; ++i)
                {
                    if (_clientEventListener[type][i] == listener)
                    {
                        hasRegistered = true;
                        break;
                    }
                }

                if (!hasRegistered)
                {
                    _clientEventListener[type].Add(listener);
                }
                else
                {
                    Debug.Log(string.Format("The listener({0}) has been registered for Client Event({1}).", listener, type));
                }
            }
            else
            {
                _clientEventListener.Add(type, new List<IClientEventListener>() { listener });
            }
        }
        //注销
        public void UnRegisterClientEventListener(ClientEventType type, IClientEventListener listener)
        {
            if (_clientEventListener.ContainsKey(type))
            {
                for (int i = 0; i < _clientEventListener[type].Count; ++i)
                {
                    if (_clientEventListener[type][i] == listener)
                    {
                        _clientEventListener[type].RemoveAt(i);
                        break;
                    }
                }
            }
        }
        //通知
        public void DispatchClientEvent(ClientEventType type, params object[] param)
        {
            if (_clientEventListener.ContainsKey(type))
            {
                if (_clientEventListener[type] != null)
                {
                    for (int i = _clientEventListener[type].Count - 1; i >= 0; --i)
                        _clientEventListener[type][i].OnClientEventHandler(type, param);
                }
            }
        }
    }
}
  



    


